<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\admin\model\AdminLogModel;
use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\admin\model\AdminSnModel;
use App\Http\Controllers\admin\model\AppLogModel;
use App\Http\Controllers\admin\model\AuthAdminModel;
use App\Http\Controllers\admin\model\ParentAppModel;
use App\Http\Controllers\admin\model\ParentSnModel;
use App\Http\Controllers\admin\model\StatisticsAppModel;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Support\Facades\DB;

class StatisticsService
{
    /**
     * 获取设备总数,激活总数,在线数量,解锁设备
     * @param $param
     * @return false|string
     */
    public static function querySumSn($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['id','group_id']);
        if(!$admin){
            return ToolUtil::returnJson(301,'不存在该账号');
        }
        switch ($admin['group_id']){
            case 1:
            case 2:
                $sumSn = AdminSnModel::queryWhereCount();//总设备数
                $actSn = ParentSnModel::queryCount();//激活数量
                $onlineSn = AdminSnModel::queryWhereCount(['online'=>1]);//在线设备
                $snLock = AdminSnModel::queryWhereCount(['is_lock'=>0]);//解锁设备
                break;
            case 3:
                $sumSn = ParentSnModel::queryCount(['tp.admin_id'=>$admin['id']]);
                $actSn = ParentSnModel::queryCount(['tp.admin_id'=>$admin['id']]);
                $onlineSn = ParentSnModel::queryCount(['tp.admin_id'=>$admin['id'],'as.online'=>1]);
                $snLock = ParentSnModel::queryCount(['tp.admin_id'=>$admin['id'],'as.is_lock'=>0]);
                break;
            case 4:
                $sumSn = ParentSnModel::queryCount(['tp.teacher_id'=>$admin['id']]);
                $actSn = ParentSnModel::queryCount(['tp.teacher_id'=>$admin['id']]);
                $onlineSn = ParentSnModel::queryCount(['tp.teacher_id'=>$admin['id'],'as.online'=>1]);
                $snLock = ParentSnModel::queryCount(['tp.teacher_id'=>$admin['id'],'as.is_lock'=>0]);
                break;
            default :
                $sumSn = 0;
                $actSn = 0;
                $onlineSn = 0;
                $snLock = 0;
                break;
        }
        $data['sumSn'] = $sumSn;
        $data['actSn'] = $actSn;
        $data['onlineSn'] = $onlineSn;
        $data['snLock'] = $snLock;
        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 应用上传数量统计
     * @param $param
     * @return false|string
     */
    public static function appUploadStatistics($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['id','group_id']);
        if(!$admin){
            return ToolUtil::returnJson(301,'不存在该账号');
        }
        $where['al.remark'] = '上传应用';
        if($admin['group_id']==1 && $admin['group_id']==2){
            $data['appCount'] = AdminLogModel::queryCount($where);
        }else{
            $where['al.admin_id'] = $param['userId'];
            $data['appCount'] = AdminLogModel::queryCount($where);
        }
        if($data['appCount']){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 统计家长app七天使用时长
     * @param $param
     * @return false|string
     */
    public static function queryAppOnlineTime($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if(!$admin){
            return ToolUtil::returnJson(301,'不存在该账号');
        }
        if($admin['group_id']!=4){
            return ToolUtil::returnJson(400,'没有数据');
        }
        $app = ParentAppModel::queryGet(['admin_id'=>$param['userId']],['id','app_package']);
        $endTime = strtotime(date('yy-m-d',time()));
        $begTime = $endTime-7*24*60*60;
        if($app){
            $arr = [];
            foreach ($app as $k=>$v){
                $v['appSum'] = AppLogModel::queryWhereSum(['parent_app_id'=>$v['id']],['begTime'=>$begTime,'endTime'=>$endTime],'minute');
                $arr[$k] = $v;
            }
            return ToolUtil::returnJson(200,'成功',$arr);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 统计有效用户和黑洞用
     * @return false|string
     */
    public static function statisticsUser(){
        $admin = AdminModel::queryGet([],['id']);
        $parent = ParentSnModel::queryGet([],['admin_id as id']);
        if($admin){
            $arr = [];
            $arr1 = [];
            foreach ($admin as $k=>$v){
                $arr[$k] = $v['id'];
            }
            foreach ($parent as $k=>$v){
                $arr1[$k] = $v['id'];
            }
            $effective = count($arr1);
            $invalid = array_diff($arr,$arr1);
            $data['effective'] = $effective;
            $data['invalid'] = count($invalid);
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 累计注册用户和今日注册用户
     * @return false|string
     */
    public static function registerUser(){
        $where = ['remark'=>'注册'];
        $cRegisterUser = AdminLogModel::queryRegisterCount($where,[]);
        $begTime = strtotime(date('yy-m-d',time()));
        $endTime = $begTime+24*60*60;
        $dRegisterUser = AdminLogModel::queryRegisterCount($where,['begTime'=>$begTime,'endTime'=>$endTime]);
        $data['cRegisterUser'] = $cRegisterUser;
        $data['dRegisterUser'] = $dRegisterUser;
        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 累计访问量和今日访问量
     * @return false|string
     */
    public static function webVisitUser(){
        $where = ['remark'=>'登录'];
        $cVisitUser = AdminLogModel::queryLoginCount($where,[]);
        $begTime = strtotime(date('yy-m-d',time()));
        $endTime = $begTime+24*60*60;
        $dVisitUser = AdminLogModel::queryLoginCount($where,['begTime'=>$begTime,'endTime'=>$endTime]);
        $data['cVisitUser'] = $cVisitUser;
        $data['dVisitUser'] = $dVisitUser;
        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 当月限制流量和当月已使用流量
     * @param $param
     * @return false|string
     */
    public static function statisticsFlow($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $begTime = date('Y-m-01', strtotime(date("Y-m-d")));
        $endTime = date('Y-m-d', strtotime("$begTime +1 month -1 day"));
        $begTime = strtotime($begTime)-8*60*60;
        $endTime = strtotime($endTime)-8*60*60;
        if($admin['group_id']==1 || $admin['group_id']==2){
            $monthFlow = AdminModel::queryWhereSum([],'month_flow');
            $useMonthFlow = round(StatisticsAppModel::queryWhereSum([],
                ['begTime'=>$begTime,'endTime'=>$endTime],'app_size')/1024/1024/1024,2);
        }else{
            $monthFlow = AdminModel::queryWhereSum(['id'=>$param['userId']],'month_flow');
            $useMonthFlow = round(StatisticsAppModel::queryWhereSum(['admin_id'=>$param['userId']],
                ['begTime'=>$begTime,'endTime'=>$endTime],'app_size')/1024/1024/1024,2);
        }
        $data['monthFlow'] = round($monthFlow,2);
        $data['useMonthFlow'] = $useMonthFlow;
        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 获取流量统计信息
     * @param $param
     * @return false|string
     */
    public static function queryStatistics($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin){
            if($admin['group_id']==1 || $admin['group_id']==2){
                $param['field'] = 'username';
                $count = AdminModel::queryCount([],$param);
                $page = ToolUtil::calculationPage($param,$count);
                $field = ['id','username','create_time','month_flow'];
                if($param['type']==1){
                    $data = DB::table('t_admin')->offset($page['startSize'])
                        ->limit($page['pageSize'])->get($field);
                }else{
                    $data = DB::table('t_admin')->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                        ->offset($page['startSize'])->limit($page['pageSize'])->get($field);
                }
                if(count($data)){
                    $result = [];
                    $begTime = date('Y-m-01', strtotime(date("Y-m-d")));
                    $endTime = date('Y-m-d', strtotime("$begTime +1 month -1 day"));
                    $begTime = strtotime($begTime)-8*60*60;
                    $endTime = strtotime($endTime)-8*60*60;
                    foreach ($data as $k=>$v){
                        $v['userTotal'] = round(DB::table('t_statistics_app')->where(['admin_id'=>$v['id']])->sum('app_size')/1024/1024/1024,2);
                        $v['userMonth'] = round(DB::table('t_statistics_app')->where('create_time','>=',$begTime)
                                ->where('create_time','<=',$endTime)
                                ->where(['admin_id'=>$v['id']])
                                ->sum('app_size')/1024/1024/1024,2);
                        $result[$k] = $v;
                    }
                    return ToolUtil::returnJson(200,'成功',$result,$count);
                }
            }
            return ToolUtil::returnJson(400,'没有数据');
        }
        return ToolUtil::returnJson(301,'不存在该用户');
    }

    /**
     * 编号获取流量统计信息
     * @param $param
     * @return false|string
     */
    public static function queryIdStatistics($param){
        $field = ['id','username','month_flow'];
        $data = AdminModel::queryFirst(['id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑用户当月限制流量
     * @param $param
     * @return false|string
     */
    public static function editStatistics($param){
        $data = [
            'month_flow'=>$param['monthFlow'],
            'update_time'=>time()
        ];
        $int = AdminModel::edit(['id'=>$param['id']],$data);
        if($int){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }
}